package com.lucker.common.util;


import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class EncryptUtil {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(EncryptUtil.class);

	private EncryptUtil() {
		super();
	}

	/**
	 * <dl>
	 * <dt>说明：加密文本</dt>
	 * <dd>如果为空返回null</dd>
	 * </dl>
	 * @date 2016年4月2日 下午12:16:38
	 * @Title:encrypt
	 * @param content 需要加密的内容
	 * @param password 加密密码
	 * @return String
	 */
	public static String encrypt(String content, String password) {
		return content == null ? null : Base64.encodeBase64String(encryptByte(content, password));
	}

	/**
	 * <dl>
	 * <dt>说明：加密文本</dt>
	 * <dd>如果为空返回null</dd>
	 * </dl>
	 * @date 2016年4月2日 下午12:16:38
	 * @Title:encryptByte
	 * @param content 需要加密的内容
	 * @param password 加密密码
	 * @return byte[]
	 */
	public static byte[] encryptByte(String content, String password) {
		try {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
			secureRandom.setSeed(password.getBytes());
			kgen.init(128, secureRandom);
			//			kgen.init(128, new SecureRandom(password.getBytes()));
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES");// 创建密码器
			byte[] byteContent = content.getBytes("utf-8");
			cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
			return cipher.doFinal(byteContent);// 加密
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			LOGGER.error("{}-{}", content, e.getMessage(), e);
		} catch (IllegalBlockSizeException e) {
			LOGGER.error("内容未加密：{}-{}", content, e.getMessage());
		} catch (BadPaddingException e) {
			LOGGER.error("{}-{}", content, e.getMessage(), e);
		}
		return null;
	}

	/**
	 * <dl>
	 * <dt>说明：解密密文</dt>
	 * <dd>如果为空返回null</dd>
	 * </dl>
	 * @author liujinlei
	 * @Title:decrypt
	 * @param content 需要加密的内容
	 * @param password 加密密码
	 * @return String
	 */
	public static String decrypt(String content, String password) {
		return content == null ? null : new String(decryptByte(Base64.decodeBase64(content), password));
	}

	/**
	 * <dl>
	 * <dt>说明：解密密文</dt>
	 * <dd>如果为空返回null</dd>
	 * </dl>
	 * @date 2016年4月2日 下午12:16:38
	 * @Title:decryptByte
	 * @param content 需要加密的内容
	 * @param password 加密密码
	 * @return byte[]
	 */
	public static byte[] decryptByte(byte[] content, String password) {
		try {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
			secureRandom.setSeed(password.getBytes());
			kgen.init(128, secureRandom);
			//			kgen.init(128, new SecureRandom(password.getBytes()));
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES");// 创建密码器
			cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
			byte[] result = cipher.doFinal(content);
			return result; // 加密
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			LOGGER.error("内容未加密：{}-{}", Base64.encodeBase64(content), e.getMessage());
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		String sign = "IamIronman'sfan!";
		//加密   
		//System.out.println("加密前：" + content);
		//String tt4 = encrypt(content, password);
		//System.out.println(tt4);

		//解密   
		//System.out.println("解密后：" + decrypt(tt4, password));
		
		
		
		
		
		
		
		String mingwen= "{\"appType\":\"androidTv\",\"caiId\":\"101\",\"userToken\":\"\",\"versionCode\":\"1\"}";
		String miwen = encrypt(mingwen, sign);
		
		System.out.println(miwen);
		
		String mingwen2 = decrypt(miwen, sign);
		System.out.println(mingwen2);
		
		
		
		System.out.println(StringUtil.isNumeric("002AA"));;
	}
	
	

}
